<?php
/**
* @backupGlobals disabled
* @backupStaticAttributes disabled
*/

include (__DIR__.'/../index.php');

class data extends \libs\PHPCan\MysqlActions
{
	public function testPostsSelectInsertSelect () {
		$result = $this->Db->select(array(
			'table' => 'posts',
			'fields' => 'id'
		));

		$this->assertEquals(0, count($result));

		for ($i = 0; $i < 10; $i++) {
			$result = $this->insertPosts();

			$this->assertEquals($i + 1, $result);
		}

		$result = $this->Db->select(array(
			'table' => 'posts',
			'fields' => 'id'
		));

		$this->assertEquals(10, count($result));

		$result = $this->Db->select(array(
			'table' => 'posts',
			'fields' => 'id',
			'limit' => '5'
		));

		$this->assertEquals(5, count($result));
	}

	public function testPostsSelectDeleteSelect () {
		$result = $this->Db->select(array(
			'table' => 'posts',
			'fields' => 'id'
		));

		$this->assertEquals(10, count($result));

		for ($i = 0; $i < 9; $i++) {
			$result = $this->deletePosts();

			$this->assertEquals(true, ((int)$result > 0));
		}

		$result = $this->Db->select(array(
			'table' => 'posts',
			'fields' => 'id'
		));

		$this->assertEquals(1, count($result));
	}

	public function testUsersSelectInsertSelect () {
		$result = $this->Db->select(array(
			'table' => 'users',
			'fields' => 'id'
		));

		$this->assertEquals(0, count($result));

		for ($i = 0; $i < 10; $i++) {
			$result = $this->insertUsers();

			$this->assertEquals($i + 1, $result);
		}

		$result = $this->Db->select(array(
			'table' => 'users',
			'fields' => 'id'
		));

		$this->assertEquals(10, count($result));
	}

	public function testUsersSelectDeleteSelect () {
		$result = $this->Db->select(array(
			'table' => 'users',
			'fields' => 'id'
		));

		$this->assertEquals(10, count($result));

		for ($i = 0; $i < 9; $i++) {
			$result = $this->deleteUsers();

			$this->assertEquals(true, ((int)$result > 0));
		}

		$result = $this->Db->select(array(
			'table' => 'users',
			'fields' => 'id'
		));

		$this->assertEquals(1, count($result));
	}

	public function testCommentsSelectInsertSelect () {
		$result = $this->Db->select(array(
			'table' => 'comments',
			'fields' => 'id'
		));

		$this->assertEquals(0, count($result));

		for ($i = 0; $i < 10; $i++) {
			$result = $this->insertComments();

			$this->assertEquals($i + 1, $result);
		}

		$result = $this->Db->select(array(
			'table' => 'comments',
			'fields' => 'id'
		));

		$this->assertEquals(10, count($result));
	}

	public function testCommentsSelectDeleteSelect () {
		$result = $this->Db->select(array(
			'table' => 'comments',
			'fields' => 'id'
		));

		$this->assertEquals(10, count($result));

		for ($i = 0; $i < 9; $i++) {
			$result = $this->deleteComments();

			$this->assertEquals(true, ((int)$result > 0));
		}

		$result = $this->Db->select(array(
			'table' => 'comments',
			'fields' => 'id'
		));

		$this->assertEquals(1, count($result));
	}

	public function testRelate ($test = true) {
		$this->resetTables(10);

		$contents = $this->getContents('all');

		$posts = $contents['posts'];
		$comments = $contents['comments'];
		$users = $contents['users'];
		$categories = $contents['categories'];

		unset($contents);

		foreach ($posts as $posts_value) {
			shuffle($users);

			$users_value = $users[0];

			shuffle($categories);

			$categories_value = array_chunk($categories, rand(1, 3));
			$categories_value = arrayKeyValues($categories_value[0], 'id');

			$comments_value = array_shift($comments);

			$this->relatePosts($posts_value['id'], $users_value['id'], $comments_value['id'], $categories_value);
		}

		$posts = $this->getContents('posts', true);
		$posts = $posts['posts'];

		if (!$test) {
			return;
		}

		foreach ($posts as $posts_value) {
			$this->assertEquals(1, count($posts_value['comments']));
			$this->assertEquals(5, count($posts_value['users']));
			$this->assertLessThan(4, count($posts_value['categories']));
			$this->assertGreaterThan(0, count($posts_value['categories']));
		}
	}

	public function testDeletePosts () {
		$this->testRelate();

		$contents = $this->getContents(array('posts', 'comments'), true);

		$posts = $contents['posts'];
		$comments = $contents['comments'];

		unset($contents);
	
		$total_comments = count($comments);

		$this->assertEquals(10, $total_comments);

		foreach ($posts as $posts_value) {
			$this->deletePosts(array('id' => $posts_value['id']), 1);

			$total_comments -= count($posts_value['comments']);

			$comments = $this->Db->select(array(
				'table' => 'comments',
				'fields' => 'id'
			));

			$this->assertEquals($total_comments, count($comments));
		}

		$this->assertEquals($total_comments, 0);
	}

	public function testDeleteUsers () {
		$this->testRelate();

		$contents = $this->getContents('all', true);

		$posts = $contents['posts'];
		$comments = $contents['comments'];
		$users = $contents['users'];

		unset($contents);
	
		$total_comments = count($comments);
		$total_posts = count($posts);

		$this->assertEquals(10, $total_comments);
		$this->assertEquals(10, $total_posts);

		foreach ($users as $users_value) {
			$this->deleteUsers(array('id' => $users_value['id']), 1);

			$total_comments -= count($users_value['comments']);
			$total_posts -= count($users_value['posts']);

			$comments = $this->Db->select(array(
				'table' => 'comments',
				'fields' => 'id'
			));

			$posts = $this->Db->select(array(
				'table' => 'comments',
				'fields' => 'id'
			));

			$this->assertEquals($total_comments, count($comments));
			$this->assertEquals($total_posts, count($posts));
		}

		$this->assertEquals($total_comments, 0);
		$this->assertEquals($total_posts, 0);
	}

	public function testPagination () {
		$this->resetTables(20);

		foreach (array('posts', 'users', 'comments') as $table) {
			$result = $this->Db->select(array(
				'table' => $table,
				'fields' => 'id',
				'limit' => '5',
				'pagination' => array(
					'page' => 1,
					'map' => 5
				)
			));

			$this->assertEquals(5, count($result));

			global $Data;

			$pagination = $Data->pagination;

			$this->assertEquals(20, $pagination['total']);
			$this->assertEquals(4, $pagination['total_pages']);
		}
	}

	public function testTotals ($Db = '') {
		parent::testTotals($this->Db);
	}
}
